package gui.dialog;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

import game.resources.*;
import main.Antiquity;

/**
 *	@(#)DiscardDialog.java
 *
 *	The <code>DiscardDialog</code> class asks a player to discard resources
 *	he cannot store due to a lack of enough storage capacity.
 *
 *	@author Grzegorz Kobiela
 *	@version 1.0 2008/4/26
 */
public final class DiscardDialog
extends JDialog
{
	/** The resource observer. */
	private ResourceObservable resLeft;

	/**
	 *	Creates a new <code>DiscardDialog</code> instance.
	 *	@param owner the owner frame
	 *	@param player the player
	 *	@param n the amount of resources
	 */
	public DiscardDialog(final gui.GameWindow owner, game.Player player, int n) {
		super(owner, String.format(Antiquity.get("dlgDiscardTitle"), player.getName()), true);
		resLeft = new ResourceObservable(n);

		/* Dialog window */
		Container contentPane = getContentPane();
		contentPane.setLayout(null);
		contentPane.setBounds((Rectangle)Antiquity.getProperty("ResourceDialogInnerBounds"));

		/* Status bar */
		final JLabel status = new JLabel();
		status.setBounds((Rectangle)Antiquity.getProperty("ResourceDialogStatusBounds"));
		status.setText(getStatusText());
		contentPane.add(status);

		/* Resource box */
		Point padding = (Point)Antiquity.getProperty("ResourceDialogBoxPadding");
		JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, padding.x, padding.y));
		mainPanel.setBounds((Rectangle)Antiquity.getProperty("ResourceDialogBoxBounds"));
		mainPanel.setBorder(BorderFactory.createLoweredBevelBorder());
		mainPanel.setBackground(Antiquity.getColor("ResourceDialogBox"));
		contentPane.add(mainPanel);

		/* Create resource panels */
		ResourceType[] types = ResourceType.values();
		JPanel[] panels = new JPanel[types.length];
		for(ResourceType type: types) {
			JPanel panel = createPanel(player, type);
			panels[type.ordinal()] = panel;
			if(panel != null) mainPanel.add(panel);
		}
		resLeft.addObserver(
			new Observer() {
				public void update(Observable obs, Object obj) {
					status.setText(getStatusText());
					if(resLeft.getValue() == 0) {
						setVisible(false);
						dispose();
					}
				}
			}
		);

		/* Dialog properties */
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(
			new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					owner.showWarning(DiscardDialog.this, Antiquity.get("errNoClose"));
				}
			}
		);
		Rectangle bounds = (Rectangle)Antiquity.getProperty("ResourceDialogBounds");
		setLocation(
			owner.getX() + (owner.getWidth() - bounds.width) / 2,
			owner.getY() + (owner.getHeight() - bounds.height) / 2
		);
		setSize(bounds.getSize());
		setResizable(false);
		setVisible(true);
	}

	/**
	 *	Returns a panel displaying all the resources of the given type
	 *	for the given player.
	 *	@param player the player
	 *	@param type the resource type
	 *	@return the panel
	 */
	private JPanel createPanel(final game.Player player, final ResourceType type) {
		int n = player.getResources(type).size();
		if(n == 0) return null;
		final JPanel panel = new JPanel(null);
		panel.setPreferredSize(((Rectangle)Antiquity.getProperty("ResourceDialogPanelBounds")).getSize());
		panel.setOpaque(false);
		Point offset = (Point)Antiquity.getProperty("ResourceDialogPanelOffset");
		Point step = (Point)Antiquity.getProperty("ResourceDialogPanelStep");
		for(int i = 0; i < n; i++) {
			ImageIcon icon = gui.GameUtilities.createIcon(Resource.createResource(type));
			JButton button = new JButton(icon);
			button.setSize(icon.getIconWidth(), icon.getIconHeight());
			button.setLocation(offset.x + step.x * i, offset.y + step.y * i);
			button.setPreferredSize(button.getSize());
			button.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Antiquity.remove(player, type);
						resLeft.decrease();
						panel.remove(panel.getComponent(panel.getComponentCount() - 1));
						panel.invalidate();
						panel.repaint();
					}
				}
			);
			panel.add(button);
		}
		return panel;
	}

	/**
	 *	Returns the text for the status bar.
	 *	@return the text
	 */
	private String getStatusText() {
		int n = resLeft.getValue();
		String left = Antiquity.get("resource", n);
		return String.format(Antiquity.get("dlgDiscardMessage"), n, left);
	}
}